var app = getApp();
Page({
  data: {
    
  },
  onLoad: function (e) {
    var that = this;
    if (e && e.enterfrom) {
      var enterfrom = e.enterfrom == 'share' ? true : false;
      that.setData({
        enterfrom: enterfrom
      })
    }
    if(e&&e.title){
      that.setData({
        title:e.title
      })
    }
    var title = that.data.title ? that.data.title:'推广海报';
    app.setNavtitle(title);
    that.requestExtendinfo();
  },
  requestExtendinfo: function () {
    var that = this;
    var memberInfo = app.globalData.memberInfo;
    console.log(memberInfo);
    var data = {};
    data.map = 'applet_three_share_code_new';
    // data.path = '/pages/distributionTip/distributionTip?mid=' + memberInfo.mid;
    //发起请求，获取列表列表
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          that.setData({
            extendInfo: res.data.data
          })
          var posterImg = that.data.extendInfo.spreadImage;
          var mid = app.globalData.memberInfo.mid;
          var posterInfo = {
            posterImg: posterImg,
            mid: mid
          }
          that.setData({
            posterInfo: posterInfo
          })
          app.globalData.posterInfo = posterInfo;
          if (!posterImg) {
            app.errorTip(that, "暂未添加海报图片", 2000);
            return;
          } 
          // posterImg
          // wx.downloadFile({
          //   url: posterImg,
          //   success: function (res) {
          //     console.log(res);
          //     that.setData({
          //       tempFilePathShow: res.tempFilePath
          //     })
          //     wx.hideLoading();
          //   },
          //   fail:function(res){
          //     console.log(res);
          //   }
          // })
        } else {
          wx.hideLoading();
          app.errorTip(that, res.data.em, 2000);
        }
      },
      fail:function(){
        wx.hideLoading();
      },
      complete: function () {
        wx.hideLoading();
        wx.stopPullDownRefresh();
      }
    });
  },
  saveImage:function(){
    var that = this;
		var imgpath = this.data.posterInfo.posterImg;
    // var tempFilePathShow = this.data.tempFilePathShow;
    if (imgpath.length<=0){
      app.errorTip(that, "海报图片不存在", 2000);
      return;
    }
    wx.showLoading({
      title: '正在保存',
      mask: true,
      time: 100000
    })
    wx.downloadFile({
      url: imgpath,
      success: function (res) {
        console.log(res);
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success(res) {
            console.log(res);
            app.errorTip(that, "图片保存成功", 2000);
          },
          fail(f) {
            // app.errorTip(that, "图片保存失败", 2000);
            wx.getSetting({
              success(res) {
                console.log(res.authSetting)
                if (!res.authSetting['scope.writePhotosAlbum']) {
                  that.setData({
                    isShowPhotosAlbum: true
                  })
                  wx.hideLoading();
                } else {
                  app.errorTip(that, "图片保存失败", 2000);
                  wx.hideLoading();
                }
              },
              fail(res) {
                console.log(res)
                wx.hideLoading();
              }
            })
          },
          complete() {
            wx.hideLoading();
          }
        })
      },
      fail: function (res) {
        console.log(res);
        app.errorTip(that, "下载失败", 2000);
      }
    })
  },
  hidePhotosAlbum:function(){
    var that = this;
    that.setData({
      isShowPhotosAlbum: false
    })
  },
  opensetting:function(e){
    console.log(e);
    var that = this;
    if (e.detail.authSetting['scope.writePhotosAlbum']){
      that.hidePhotosAlbum();
    }
  },
	requestPoster: function () {
		var that = this;
		var memberInfo = app.globalData.memberInfo;
		var data = {};
		data.map = 'applet_three_reset_spread_new';
		//发起请求，获取列表列表
		wx.showToast({
			title: '加载中',
			icon: 'loading',
			mask: true,
			duration: 10000
		});
		wx.request({
			url: app.globalData.requestUrl,
			data: data,
			success: function (res) {
				console.log(res.data);
				if (res.data.ec == 200) {
          var mid = app.globalData.memberInfo.mid;
          var posterInfo = {
            posterImg: res.data.data.spreadImage,
            mid: mid
          }
          that.setData({
            posterInfo: posterInfo
          })
					// wx.downloadFile({
					// 	url: res.data.data.spreadImage,
					// 	success: function (res) {
					// 		console.log(res);
					// 		that.setData({
					// 			tempFilePathShow: res.tempFilePath
					// 		})
					// 		app.errorTip(that, "销毁重建成功", 2000);
					// 	}
					// })
					console.log(that.data.posterInfo);
          app.errorTip(that, "销毁重建成功", 2000);
					
				} else {
					app.errorTip(that, res.data.em, 2000);
				}
			},
			complete: function () {
				wx.hideToast();
				wx.stopPullDownRefresh();
			}
		});
	},
  onShareAppMessage: function () {
    var that = this;
    var mid = app.globalData.memberInfo.mid;
    console.log('用户mid'+mid);
    var title = that.data.title;
    var cover = that.data.extendInfo.spreadImage;
    app.getPoint(that);
    return {
      title: title,
      imageUrl: cover,
      // path: '/pages/distributionTip/distributionTip?enterfrom=share&scene=' + mid
      // path: '/pages/singlePage/singlePage?enterfrom=share&scene=' + mid
      path: '/subpages0/distributionCenter/distributionCenter'
    }
  }
})